/*!
    \file    gd32f10x_it.c
    \brief   interrupt service routines

    \version 2024-01-05, V2.3.0, firmware for GD32F10x
*/

/*
    Copyright (c) 2024, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "gd32f10x_it.h"
#include "main.h"
#include "systick.h"


/*!
    \brief      this function handles NMI exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void NMI_Handler(void)
{
    /* if NMI exception occurs, go to infinite loop */
    while(1)
    {
    }
}

/*!
    \brief      this function handles HardFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void HardFault_Handler(void)
{
    /* if Hard Fault exception occurs, go to infinite loop */
    while(1)
    {
    }
}

/*!
    \brief      this function handles MemManage exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void MemManage_Handler(void)
{
    /* if Memory Manage exception occurs, go to infinite loop */
    while(1)
    {
    }
}

/*!
    \brief      this function handles BusFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void BusFault_Handler(void)
{
    /* if Bus Fault exception occurs, go to infinite loop */
    while(1)
    {
    }
}

/*!
    \brief      this function handles UsageFault exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void UsageFault_Handler(void)
{
    /* if Usage Fault exception occurs, go to infinite loop */
    while(1)
    {
    }
}

/*!
    \brief      this function handles SVC exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SVC_Handler(void)
{
    extern void vPortSVCHandler(void);
    extern BaseType_t xTaskGetSchedulerState(void);

    /* call FreeRTOS SVC handler if FreeRTOS secheduler is started,
       otherwise go to infinite loop*/
    if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
    {
        /* call FreeRTOS SVC handler */
        vPortSVCHandler();
    }
    else
    {
        /* if SVC exception occurs, go to infinite loop */
        while(1)
        {
        }
    }
}

/*!
    \brief      this function handles DebugMon exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DebugMon_Handler(void)
{
    /* if DebugMon exception occurs, go to infinite loop */
    while(1)
    {
    }
}

/*!
    \brief      this function handles PendSV exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void PendSV_Handler(void)
{
    extern void xPortPendSVHandler(void);
    extern BaseType_t xTaskGetSchedulerState(void);

    /* call FreeRTOS PendSV handler if FreeRTOS secheduler is started,
       otherwise go to infinite loop */
    if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
    {
        /* call FreeRTOS PendSV handler */
        xPortPendSVHandler();
    }
    else
    {
        /* if PendSV exception occurs, go to infinite loop */
        while(1)
        {
        }
    }
}

/*!
    \brief      this function handles SysTick exception
    \param[in]  none
    \param[out] none
    \retval     none
*/
void SysTick_Handler(void)
{
    extern void xPortSysTickHandler(void);
    extern BaseType_t xTaskGetSchedulerState(void);
    /* system already start. make sure system scheduler is setup before call
     xPortSysTickHandler, otherwise this is wille access the null pointer,
     triggering a hard fault because task switch maybe occur before the scheduler
     steup. */
    if(xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
    {
        xPortSysTickHandler();
    }
}


/******************************* Peripheral Interrupt Handler *******************************/

void usart_rx_cplt_callbake_it(uint32_t usart_periph);
void usart_idle_callbake_it(uint32_t usart_periph);
void exti_5_9_callbake_it(uint32_t exti_periph);
void exti_10_15_callbake_it(uint32_t exti_periph);
void adc_eoc_callback_it(uint32_t adcx);
void adc_eoic_callback_it(uint32_t adcx);
void dma_transfer_complete_callback_it(uint32_t dma_periph, dma_channel_enum channel);
void dma_transfer_half_callback_it(uint32_t dma_periph, dma_channel_enum channel);
void timer_update_callback_it(uint32_t timer_periph);


/**
 * @brief usart0 interrupt handler
 * @param None
 */
void USART0_IRQHandler(void)
{
    /* usart0 receive complete interrupt */
    if(SET == usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
    {
        /* clear RBNE interrupt flag */
        usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
        /* call complete callbake function */
        usart_rx_cplt_callbake_it(USART0);
    }
    /* usart0 idle interrupt */
    if(SET == usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE))
    {
        /* read the data register to the idle flag */
        USART_DATA(USART0);
        /* call idle callbake function */
        usart_idle_callbake_it(USART0);
    }
}

/**
 * @brief external interrupt line 5~9 handler
 * @param none
 */
void EXTI5_9_IRQHandler(void)
{
    if(RESET != exti_interrupt_flag_get(EXTI_8))
    {
        /* clear the exti line 5 interrupt flag */
        exti_interrupt_flag_clear(EXTI_8);
        /* call callbake function */
        exti_5_9_callbake_it(EXTI_8);
    }
}

/**
 * @brief external interrupt line 10~15 handler
 * @param none
 */
void EXTI10_15_IRQHandler(void)
{
    /* line 12 interrupt */
    if(RESET != exti_interrupt_flag_get(EXTI_12))
    {
        /* clear the exti line 12 interrupt flag */
        exti_interrupt_flag_clear(EXTI_12);
        /* call callbake function */
        exti_10_15_callbake_it(EXTI_INTEN_INTEN12);
    }
    /* line 13 inerrupt */
    else if(RESET != exti_interrupt_flag_get(EXTI_13))
    {
        /* clear the exti line 13 interrupt flag */
        exti_interrupt_flag_clear(EXTI_13);
        /* call callbake function */
        exti_10_15_callbake_it(EXTI_INTEN_INTEN13);
    }
    /* line 14 inerrupt */
    else if(RESET != exti_interrupt_flag_get(EXTI_14))
    {
        /* clear the exti line 14 interrupt flag */
        exti_interrupt_flag_clear(EXTI_14);
        /* call callbake function */
        exti_10_15_callbake_it(EXTI_INTEN_INTEN14);
    }
    /* line 15 interrupt */
    else if(RESET != exti_interrupt_flag_get(EXTI_15))
    {
        /* clear the exti line 15 interrupt flag */
        exti_interrupt_flag_clear(EXTI_15);
        /* call callbake function */
        exti_10_15_callbake_it(EXTI_INTEN_INTEN15);
    }
}

/**
 * @brief DMA0 channel0 interrupt handler
 */
void DMA0_Channel0_IRQHandler(void)
{
    if(SET == dma_interrupt_flag_get(DMA0, DMA_CH0, DMA_INT_FLAG_FTF))
    {
        dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_FTF);

        dma_transfer_complete_callback_it(DMA0, DMA_CH0);
    }
    if(SET == dma_interrupt_flag_get(DMA0, DMA_CH0, DMA_INT_FLAG_HTF))
    {
        dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_HTF);

        dma_transfer_half_callback_it(DMA0, DMA_CH0);
    }
}

/**
 * @brief DMA0 channel1 interrupt handler
 */
void DMA0_Channel1_IRQHandler(void)
{
    if(SET == dma_interrupt_flag_get(DMA0, DMA_CH1, DMA_INT_FLAG_FTF))
    {
        dma_interrupt_flag_clear(DMA0, DMA_CH1, DMA_INT_FLAG_FTF);

        dma_transfer_complete_callback_it(DMA0, DMA_CH1);
    }
}

/**
 * @brief DMA0 channel3 interrupt handler
 */
void DMA0_Channel3_IRQHandler(void)
{
    if(SET == dma_interrupt_flag_get(DMA0, DMA_CH3, DMA_INT_FLAG_FTF))
    {
        dma_interrupt_flag_clear(DMA0, DMA_CH3, DMA_INT_FLAG_FTF);

        dma_transfer_complete_callback_it(DMA0, DMA_CH3);
    }
}

/**
 * @brief ADC0 and ADC1 interrupt handler
 * @param None
 */
void ADC0_1_IRQHandler(void)
{
    if(SET == adc_interrupt_flag_get(ADC1, ADC_INT_FLAG_EOIC))
    {
        adc_interrupt_flag_clear(ADC1, ADC_INT_FLAG_EOIC);

        adc_eoc_callback_it(ADC1);
    }
}

/**
 * @brief timer0 channel interrupt handler
 */
void TIMER0_Channel_IRQHandler(void)
{
    timer_interrupt_flag_clear(TIMER0, TIMER_INT_CH0);
    __nop();
}

/**
 * @brief timer3 interrupt handler
 */
void TIMER3_IRQHandler(void)
{
    timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);

    timer_update_callback_it(TIMER3);
}

/************************ Interrupt Callbake Funcation ***********************/

/**
 * @brief usart receive complete callbake function
 * @param usart_periph  usart peripheral select
 */
__weak void usart_rx_cplt_callbake_it(uint32_t usart_periph)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief usart idle callbake function
 * @param usart_periph  usart peripheral select
 */
__weak void usart_idle_callbake_it(uint32_t usart_periph)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief exti line 10~15 interrupt handler callbake function
 * @param exti_periph  exti interrupt select
 */
__weak void exti_5_9_callbake_it(uint32_t exti_inter)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief exti line 10~15 interrupt handler callbake function
 * @param exti_periph  exti interrupt select
 */
__weak void exti_10_15_callbake_it(uint32_t exti_inter)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief adc end of group conversion interrupt handler callbake function
 * @param adcx adc select
 */
__weak void adc_eoc_callback_it(uint32_t adcx)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief adc end of inserted group conversion interrupt handler callbake function
 * @param adcx adc select
 */
__weak void adc_eoic_callback_it(uint32_t adcx)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief DMA transfer complete interrupt handler callbake function
 * @param dma_periph dma peripheral select
 * @param channel dma channel select
 */
__weak void dma_transfer_complete_callback_it(uint32_t dma_periph, dma_channel_enum channel)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief DMA transfer complete half interrupt handler callbake function
 * @param dma_periph dma peripheral select
 * @param channel dma channel select
 */
__weak void dma_transfer_half_callback_it(uint32_t dma_periph, dma_channel_enum channel)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}

/**
 * @brief 定时器更新中断回调函数
 * @param timer_periph 外设句柄
 */
__weak void timer_update_callback_it(uint32_t timer_periph)
{
    __nop();
    /**
     * @todo  the user implement the function
     */
}
